package com.ruileroi.ai.service.impl;

import com.ruileroi.ai.service.ISpringAiChatMemoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;

import java.util.List;

/**
 * @program: ruileroi-ai
 * @description:
 * @author: wangrui
 * @create: 2025-05-26 11:54
 **/

@RequiredArgsConstructor
public class DefaultChatMemory implements ChatMemory {
    private final ISpringAiChatMemoryService service;
    private final int maxSize;
    @Override
    public void add(String conversationId, List<Message> messages) {
        service.saveAll(conversationId, messages);
    }

    @Override
    public List<Message> get(String conversationId) {
        return service.findTopN(conversationId, maxSize);
    }

    @Override
    public void clear(String conversationId) {
        service.deleteByConversationId(conversationId);
    }
}
